//@author A0087652W
package net.mysoc.w111j.service.filters;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.mysoc.w111j.model.Category;
import net.mysoc.w111j.model.Expense;
import net.mysoc.w111j.model.PaymentType;
import net.mysoc.w111j.service.ExpenseService;
import net.mysoc.w111j.ui.Constants;

/**
 * Advanced search filter.
 */
public class MultiFilter implements ExpenseFilter {
    private final String searchPhrase;
    private List<ExpenseFilter> filters;
    private Oper operation;

    public MultiFilter(String searchPhrase, ExpenseService expenseService) throws ParseException {
        if (searchPhrase == null) {
            throw new NullPointerException("Search phrase should not be null");
        }
        this.searchPhrase = searchPhrase.trim();
        if (this.searchPhrase.isEmpty()) {
            throw new NullPointerException("Search phrase should not be empty");
        }

        filters = new ArrayList<>();

        String[] clauses;
        if (searchPhrase.contains("&&")) {
            operation = Oper.AND;
            clauses = searchPhrase.split("&&");
        } else if (searchPhrase.contains("||")) {
            operation = Oper.OR;
            clauses = searchPhrase.split("\\|\\|");
        } else {
            throw new ParseException("No search operators found", searchPhrase.length());
        }

        for (String clause : clauses) {
            String[] split = clause.split(":");
            String attribute = split[0];
            String value = split[1];

            ExpenseFilter filter = null;
            if (attribute.equalsIgnoreCase("category")) {
                Category category = expenseService.getCategoryByName(value);
                if (category != null) {
                    filter = new CategoryExpenseFilter(category);
                }
            } else if (attribute.equalsIgnoreCase("paymentType")) {
                PaymentType paymentType = expenseService.getPaymentTypeByName(value);
                if (paymentType != null) {
                    filter = new PaymentTypeExpenseFilter(paymentType);
                }
            } else if (attribute.equalsIgnoreCase("text")) {
                if (!value.isEmpty()) {
                    filter = new TextExpenseFilter(value);
                }
            } else if (attribute.equalsIgnoreCase("date")) {
                try {
                    String[] dates = value.split("\\.\\.");
                    Date start = Constants.DATE_FORMAT.parse(dates[0]);
                    Date end = Constants.DATE_FORMAT.parse(dates[1]);
                    filter = new DateExpenseFilter(start, end);
                } catch (ParseException e) {
                    filter = null;
                }
            }
            if (filter != null) {
                filters.add(filter);
            } else {
                throw new ParseException("Could not parse expression", searchPhrase.length());
            }
        }
    }

    @Override
    public boolean shouldSelect(Expense expense) {
        boolean result = filters.get(0).shouldSelect(expense);
        for (int i = 1; i < filters.size(); i++) {
            if (operation == Oper.AND) {
                result = result && (filters.get(i).shouldSelect(expense));
            } else {
                result = result || (filters.get(i).shouldSelect(expense));
            }
        }
        return result;
    }

    @Override
    public String getDescription() {
        return "Results for \"" + searchPhrase + "\"";
    }

    private enum Oper {
        AND, OR
    }
}
